﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GanagroLite
{
    public partial class PopupHtml : Form
    {
        public DataTable BrowserDataSource { get; set; }
        private string _title;
        private string _parameters;

        public PopupHtml(DataTable dt, string Title, string Parameters)
        {
            InitializeComponent();
            BrowserDataSource = dt;
            _title = Title;
            _parameters = Parameters;
        }

        private void PopupHtml_Activated(object sender, EventArgs e)
        {
            this.webBrowser1.DocumentText = ConvertToHtml(BrowserDataSource);
        }

        private void DisplayDataTable(DataTable dTable)
        {

            webBrowser1.DocumentText = "<html><body></body></html>";

            if (webBrowser1.Document != null)
            {
                HtmlElement tableRow = null;
                HtmlElement headerElem = null;

                HtmlDocument doc = webBrowser1.Document;
                HtmlElement tableElem = doc.CreateElement("TABLE");
                doc.Body.AppendChild(tableElem);

                HtmlElement tableHeader = doc.CreateElement("THEAD");
                tableElem.AppendChild(tableHeader);
                tableRow = doc.CreateElement("TR");
                tableHeader.AppendChild(tableRow);

                foreach (DataColumn col in dTable.Columns)
                {
                    headerElem = doc.CreateElement("TH");
                    headerElem.InnerText = col.ColumnName;
                    tableRow.AppendChild(headerElem);
                }

                // Create table rows.
                HtmlElement tableBody = doc.CreateElement("TBODY");
                tableElem.AppendChild(tableBody);
                foreach (DataRow dr in dTable.Rows)
                {
                    tableRow = doc.CreateElement("TR");
                    tableBody.AppendChild(tableRow);
                    foreach (DataColumn col in dTable.Columns)
                    {
                        Object dbCell = dr[col];
                        HtmlElement tableCell = doc.CreateElement("TD");
                        if (!(dbCell is DBNull))
                        {
                            tableCell.InnerText = dbCell.ToString();
                        }
                        tableRow.AppendChild(tableCell);
                    }
                }
            }
        }

        public string ConvertToHtml(DataTable sentDataTable)
        {

            // Check if the Sent DataTable is not empty or a Null
            if (sentDataTable == null)
            {
                throw new System.ArgumentNullException("sentDataTable");
            }


            //Get a worker object.
            StringBuilder myStringBuilder = new StringBuilder();


            //Open tags and write the top portion.
            myStringBuilder.Append("<html xmlns='http://www.w3.org/1999/xhtml'>");
            myStringBuilder.Append("<head>");
            myStringBuilder.Append("<title>");
            myStringBuilder.Append(_title);
            myStringBuilder.Append(_parameters);
            myStringBuilder.Append("Page-");
            myStringBuilder.Append(Guid.NewGuid().ToString());
            myStringBuilder.Append("</title>");
            myStringBuilder.Append("</head>");
            myStringBuilder.Append("<body>");
            myStringBuilder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
            myStringBuilder.Append("style='border: solid 1px Silver; font-size: x-small;'>");



            //Add the headings row.

            myStringBuilder.Append("<tr align='left' valign='top'>");

            foreach (DataColumn myColumn in sentDataTable.Columns)
            {
                myStringBuilder.Append("<td align='left' valign='top'>");
                myStringBuilder.Append(myColumn.ColumnName);
                myStringBuilder.Append("</td>");
            }

            myStringBuilder.Append("</tr>");

            //Add the data rows.
            foreach (DataRow myRow in sentDataTable.Rows)
            {
                myStringBuilder.Append("<tr align='left' valign='top'>");

                foreach (DataColumn myColumn in sentDataTable.Columns)
                {
                    myStringBuilder.Append("<td align='left' valign='top'>");
                    myStringBuilder.Append(myRow[myColumn.ColumnName].ToString());
                    myStringBuilder.Append("</td>");
                }

                myStringBuilder.Append("</tr>");
            }

            //Close tags.
            myStringBuilder.Append("</table>");
            myStringBuilder.Append("</body>");
            myStringBuilder.Append("</html>");

            return myStringBuilder.ToString();
        }

        private void buttonImprimir_Click(object sender, EventArgs e)
        {
            this.webBrowser1.Print();
        }

    }
}
